//
//  TicketOrderListVC.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/12/25.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "TicketOrderListVC.h"
#import "TicketOrderCell.h"
#import "TicketOrderDetail.h"

@interface TicketOrderListVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)NSArray *dataSource;
//@property (nonatomic) NSInteger start;

@end

@implementation TicketOrderListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setup];
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)setup {
    
    self.barBGAlpha = 0.f;
    self.view.backgroundColor = kBGColor;
    CGFloat H = kScreenHeight - kNavigationBarHeight - kTableBarHeight - 49;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, H) style:UITableViewStylePlain];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view.mas_leading);
        make.trailing.equalTo(self.view.mas_trailing);
        make.top.equalTo(self.view.mas_top);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"TicketOrderCell" bundle:nil] forCellReuseIdentifier:@"TicketOrderCell"];
    
    [self configEmptyDataTableView:self.tableView emptyImageName:@"暂无订单" describe:MYLocalizedString(@"PUBLIC_NODATA_ORDER", nil) refresh:nil block:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadDataSource) name:kLoginSuccessNotif object:nil];
    
    @weakify(self);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
//        self.start = 0;
        [self loadDataSource];
    }];
    
//    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        @strongify(self);
//        [self loadDataSource];
//    }];
    
    [self.tableView setThemeStyle:ThemeStyleBlack];
    
    if ([IGUtil hasLoginOrPresentLogin]) {
        
        [ActivityIndicatorView showHUDAddedTo:self.view];
        //        self.start = 0;
        [self loadDataSource];
    }
}

//- (NSMutableArray *)dataSource {
//
//    if (!_dataSource) {
//        _dataSource = [NSMutableArray array];
//    }
//    return _dataSource;
//}

- (void)loadDataSource {
    
    @weakify(self);
    [NetAPIManager getOrderStart:@(0) count:@(1000) type:MerchTypeTicket Block:^(NSArray<OrderModel *> *orderArray, NSError *error) {
        @strongify(self);
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        
        self.dataHasLoad = YES;
        [self.tableView.mj_header endRefreshing];
        
//        if (self.start == 0) {
//            [self.tableView.mj_footer resetNoMoreData];
//            [self.dataSource removeAllObjects];
//        }
        
//        if (orderArray.count < 10) {
//            [self.tableView.mj_footer endRefreshingWithNoMoreData];
//        } else {
//
//            [self.tableView.mj_footer endRefreshing];
//        }
        
//        [self.dataSource addObjectsFromArray:orderArray];
        
        self.dataSource = orderArray;
        
        [self.tableView reloadData];
        
//        self.start ++;
    }];
}
#pragma mark  -- UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TicketOrderCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TicketOrderCell"];
    
    OrderModel *model = self.dataSource[indexPath.section];
    
    cell.model = model;
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerView"];
    if (!headerView) {
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"headerView"];
        headerView.backgroundColor = [UIColor clearColor];
    }
    
    return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 156;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 9;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    TicketOrderDetail * detailVC = [[TicketOrderDetail alloc] init];
    OrderModel *model = self.dataSource[indexPath.section];
    
    detailVC.hidesBottomBarWhenPushed = YES;
    detailVC.ticketOrderID = model.orderid;
    
    [self.superVC.navigationController pushViewController:detailVC animated:YES];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
